package cn.itcast.store.service.serviceImp;

import java.sql.SQLException;
import java.util.List;

import cn.itcast.store.dao.CategoryDao;
import cn.itcast.store.dao.daoImp.CategoryDaoImp;
import cn.itcast.store.domain.Category;
import cn.itcast.store.service.CategoryService;
import cn.itcast.store.utils.JDBCUtils;
import cn.itcast.store.utils.JedisUtils;
import redis.clients.jedis.Jedis;

public class CategoryServiceImp implements CategoryService {
	CategoryDao  Category =new CategoryDaoImp();
	@Override
	public List<Category> findAllCats() throws SQLException {
		
		return Category.findAllCats();
	}

	@Override
	public void addCategory(Category c) {
		// TODO Auto-generated method stub	
		Jedis j = JedisUtils.getJedis();
		j.del("allCats");
		JedisUtils.closeJedis(j);
		
		
		Category.addCategory(c);
	}

	@Override
	public Category findCategoryByCid(String cid) throws SQLException {
		
		return Category.findCategoryByCid(cid);
	}

	@Override
	public void EditSaveCategory(Category category) {
		Jedis j = JedisUtils.getJedis();
		j.del("allCats");
		JedisUtils.closeJedis(j);
		Category.EditSaveCategory(category);
	}

	@Override
	public void deleteCategory(String cid) {
		Jedis j = JedisUtils.getJedis();
		j.del("allCats");
		JedisUtils.closeJedis(j);
		
		try {
			JDBCUtils.startTransaction();
			Category.setProductCid(cid);
			
			Category.deleteCategory(cid);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			JDBCUtils.rollbackAndClose();
		}
		
		JDBCUtils.commitAndClose();
		
		
		
	}

}
